package com.hoongs.v1.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hoongs.v1.dto.RiderPageDto;
import com.hoongs.v1.entity.Rider;
import com.hoongs.v1.service.RiderService;
import com.hoongs.v1.utils.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/rider")
@Validated
public class RiderController {

    @Resource
    private RiderService riderService;

    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result add(@Validated Rider rider, BindingResult bindingResult) {
        String res = Result.resultMsg(bindingResult);
        return StringUtils.isNotBlank(res) ? Result.opError(res) : riderService.add(rider);
    }

    @PostMapping("/remove")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result remove(String id) {
        return riderService.remove(id);
    }

    @PostMapping("/update")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result update(Rider rider) {
        return riderService.update(rider);
    }


    @GetMapping("/selectById")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result selectById(String id) {
        return riderService.selectById(id);
    }

    @GetMapping("/selectList")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result selectList(String name, String mobile) {
        return riderService.selectList(name, mobile);
    }

    @GetMapping("/selectPageList")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result selectPageList(RiderPageDto riderPageDto) {
        return riderService.selectPageList(riderPageDto);
    }

    @GetMapping("/selectOrderList")
    public Result selectOrderList(String status, @RequestHeader("token") String token) {
        return riderService.selectOrderList(status, token);
    }

}
